// pages/order/definite/definite.js
const post_to = require("../../module/service.js").post_to;
Page({
  data:{
    surelist:[],
    suregoods:0,
    remarkmessage:'',
    promotion: '',
    shop_id: 0,
    msg_ready: false,
    had_req: false
  },

  onLoad(){       //这里想使用真机测试onShow时页面栈的影响，可是真机无法访问电脑ip的服务器。

    let shopInfo = getApp().globalData.shopInfo;
    this.setData({
      surelist: shopInfo.curr_translist,
      suregoods: shopInfo.curr_isgoods,
      promotion: shopInfo.curr_promotion,
      shop_id: getApp().globalData.curr_shop_id
    });

  },

  handleRemark(e){
    this.setData({
      remarkmessage:e.detail.value
    });
    // console.log("remarkmessage is: ", this.data.remarkmessage);
  },

  post_order()
  {
    let userInfo = getApp().globalData.userInfo;
    console.log("remarkmessage is: ", this.data.remarkmessage);
    //对surelist进行精简
    let tmp_surelist_len = this.data.surelist.length;
    console.log(this.data.surelist);
    for (let i=0; i<tmp_surelist_len; i++)
    {
      for(let j=0; j<this.data.surelist[i].attrs.length; j++)
      {
        for (let x=0; x<this.data.surelist[i].attrs[j].attr_val.length; x++)
        {
          if (!this.data.surelist[i].attrs[j].attr_val[x].selected)
          {
            console.log(this.data.surelist[i].attrs[j].attr_val[x]);
            this.data.surelist[i].attrs[j].attr_val.splice(x, 1);
          }
        }
      }
    }

    let orderInfo = {
      user_id: userInfo.user_id,
      shop_id: this.data.shop_id,
      surelist: this.data.surelist,
      suregoods: this.data.suregoods,
      remarks: this.data.remarkmessage,
      promotion: this.data.promotion
    };

    if (!getApp().globalData.userInfo.is_login)
    {
      wx.showModal({
        title: '认证失败',
        content: '请先登录！',
        complete: (res) => {
          if (res.confirm)
            wx.navigateTo({
              url: '/pages/recored/recored',
            });
          if (res.cancel)
          {}
        }
      });
    }
    else
    {
      //将订单信息提交给服务器处理
      post_to('user/order', orderInfo).then((res)=>{
        if (res.data.msg === "error")
        {
          wx.showToast({
            title: 'ERROR',
            icon: 'error',
            duration: 1000
          });
          console.log("post error, path: user/order");
          return;
        }

        //请求成功后跳转页面
        wx.showToast({
          title: '成功，即将跳转',
          icon:'success',
          duration:1000
        });
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/detail/detail?remarks='+
              this.data.remarkmessage + '&order_num=' +
              res.data.order_num,
            // url: '/pages/detail/detail',
            // data: reqParm
          });
        }, 1000);
      }, (err)=>{
        console.log('请求错误：', err);
      });
      }
    
  },
  
  handleDetail(){
    wx.showLoading({
      title: '加载中...',
      mask:true
    });
    //延时1s，确保remarkmessage已设置好
    setTimeout(()=>{
      this.post_order();
      wx.hideLoading();
    }, 1000);
  }
})